package cn.wolfcode.reptile;

import org.apache.http.HttpEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.util.ArrayList;

public class HttpClientPost {
    public static void main(String[] args) throws Exception {
        //1.指定一个url
        String url = "https://www.tianyancha.com/login";
        //2.创建一个默认的httpclient对象 
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //3.如果是post请求那就创建一个post对象
        HttpPost httpPost = new HttpPost(url);
        //set header
        httpPost.setHeader("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36");

        ArrayList<BasicNameValuePair> basicNameValuePairs = new ArrayList<BasicNameValuePair>();
        //设置参数（设置登录名和密码实现自动登录）
        basicNameValuePairs.add(new BasicNameValuePair("username", "zhoujie"));
        basicNameValuePairs.add(new BasicNameValuePair("password", "1314520"));

        httpPost.setEntity(new UrlEncodedFormEntity(basicNameValuePairs));

        CloseableHttpResponse execute = httpClient.execute(httpPost);
        HttpEntity entity = execute.getEntity();
        String html = EntityUtils.toString(entity,"utf-8");
        System.out.println(html);
    }
}